package com.xudow.app.newui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.activeshare.edu.ucenter.models.user.UserProfileWithOther;
import com.tabview.difficultpoint.AttendanceActivity;
import com.tabview.difficultpoint.CourseDifficultPointsActivity;
import com.xudow.app.R;
import com.xudow.app.XApplication;
import com.xudow.app.newui.AskQuestionActivity;
import com.xudow.app.newui.DirectseedingActivity;
import com.xudow.app.newui.PreLoginActivity;
import com.xudow.app.newui.adapter.StudentHadapter;
import com.xudow.app.newui.base.BaseUiFragment;
import com.xudow.app.newui.bean.UserListStudentMessage;
import com.xudow.app.newui.task.StudentListTaskls;
import com.xudow.app.ui.ClazzMistakeActivity;
import com.xudow.app.ui.ClazzScheduleActivity;
import com.xudow.app.ui.MessageActivity;
import com.xudow.app.ui.task.StudentListTask;
import com.xudow.app.ui.user.QuestionsActivity;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import it.sephiroth.android.library.widget.AdapterView;
import it.sephiroth.android.library.widget.HListView;

/**
 * 作者：zhouXJ on 2016/4/5 15:12
 * 工具箱
 */

public class ToolKitFragment extends BaseUiFragment implements View.OnClickListener {

    @BindView(R.id.home_top_bar_title_name)
    TextView home_top_bar_title_name; // 标题

    @BindView(R.id.toolkit_schedule_ll)
    LinearLayout toolkit_schedule_ll;

    @BindView(R.id.toolkit_message_ll)
    LinearLayout toolkit_message_ll;

    @BindView(R.id.toolkit_error_sweeper_ll)
    LinearLayout toolkit_error_sweeper_ll;

    @BindView(R.id.toolkit_attendance_ll)
    LinearLayout toolkit_attendance_ll;

    @BindView(R.id.toolkit_difficult_points_ll)
    LinearLayout toolkit_difficult_points_ll;

    @BindView(R.id.toolkit_my_questions_ll)
    LinearLayout toolkit_my_questions_ll;

    @BindView(R.id.student_list_layout)
    LinearLayout student_list_layout;//horizontalscorller方案

    @BindView(R.id.zhibo_ll)
    LinearLayout zhibo_ll;

    @BindView(R.id.student_list)
    HListView student_hlist;//孩子横向列表

    private List<Student> studentlist=null;
    private StudentHadapter myadapter;
    private int selectStudent=0;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        return inflater.inflate(R.layout.fragment_toolkit, container, false);
    }

    @Override
    protected void initData() {
        studentlist=new ArrayList<Student>();
        home_top_bar_title_name.setText("工具箱");


        /*handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                hideLoadingDialog();
            }
        }, 2000);*/
        /*if (getApp().isLogin()){//如果已经登录
            //加载学生列表
            StudentListTask studentListTask = new StudentListTask(getMyAct(), studentListHandler);
            getMyAct().addTask(studentListTask);
            studentListTask.execute();
            showLodingDialog("正在加载");
        }*/
        myadapter=new StudentHadapter(getMyAct(),studentlist);
        student_hlist.setAdapter(myadapter);
    }

    @Override
    public void onResume() {
        super.onResume();
           Log.i("onResume","getApp().isLogin()"+getApp().isLogin());
        if (getApp().isLogin()){//如果已经登录
            //加载学生列表
          StudentListTask studentListTask = new StudentListTask(getMyAct(), studentListHandler);
            getMyAct().addTask(studentListTask);
            studentListTask.execute();

//            showLodingDialog("正在加载");
//            StudentListTaskls studentListTaskls=new StudentListTaskls(getMyAct(),studentListHandler);
//            studentListTaskls.st();
//            hideLoadingDialog();
        }
    }

    @Override
    protected void initListener() {
        toolkit_schedule_ll.setOnClickListener(this);
        toolkit_message_ll.setOnClickListener(this);
        toolkit_error_sweeper_ll.setOnClickListener(this);
        toolkit_attendance_ll.setOnClickListener(this);
        toolkit_difficult_points_ll.setOnClickListener(this);
        toolkit_my_questions_ll.setOnClickListener(this);
        zhibo_ll.setOnClickListener(this);
        student_hlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                for (Student s:studentlist){
                    s.isselect=false;
                }
                selectStudent=i;
                studentlist.get(i).isselect=true;
                myadapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    public void onClick(View view) {

        Intent intent = null;

        switch (view.getId()) {

            case R.id.toolkit_schedule_ll: // 课表

                intent = new Intent(getActivity(), ClazzScheduleActivity.class); // 课程表
                break;

            case R.id.toolkit_message_ll: // 消息箱
                intent = new Intent(getActivity(), MessageActivity.class);

                break;

            case R.id.toolkit_error_sweeper_ll: // 错题本
                intent = new Intent(getActivity(), ClazzMistakeActivity.class);
                break;

            case R.id.toolkit_attendance_ll: // 考勤
                intent = new Intent(getActivity(), AttendanceActivity.class);
                break;

            case R.id.toolkit_difficult_points_ll: // 课程重难点
                intent = new Intent(getActivity(), CourseDifficultPointsActivity.class);

                break;

            case R.id.toolkit_my_questions_ll: // 我的提问
                intent = new Intent(getActivity(), AskQuestionActivity.class);
                break;
            case R.id.zhibo_ll: // 直播课
                XApplication application = (XApplication) XApplication.getInstance();
                    if (!application.isLogin()) {

                       intent=new Intent(getActivity(), PreLoginActivity.class);

                    }else{
                intent = new Intent(getActivity(), DirectseedingActivity.class);
        }

                break;
        }

        if (null!=studentlist&&studentlist.size()>0) {
            intent.putExtra("studentId", "" + studentlist.get(selectStudent).item.getId());
        }
        startActivity(intent);

    }
    /**
    *加载学生信息handler
     */
    private Handler studentListHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            hideLoadingDialog();

            if (StudentListTask.SUCCESS == msg.what) {
                UserProfileWithOther[] studentArr=(UserProfileWithOther[])msg.getData().get("students");
                if(null!=studentArr&&studentArr.length>0) {
                    studentlist.clear();
                    for (UserProfileWithOther u : studentArr) {
                        Student s = new Student();
                        s.item = u;
                        s.isselect = false;
                        studentlist.add(s);
                    }
                    studentlist.get(0).isselect = true;
                    myadapter.setData(studentlist);
                }
               // onStudentListSuccess(msg.getData());
            } else {
                //singleStudentView.setVisibility(View.GONE);
                String message = getString(R.string.loading_fail);
                getApp().makeToast(message);
            }
        }
    };

    public class Student {
        public UserProfileWithOther item;
        public boolean isselect;
    }
}
